package com.wshl.aijiuunion.controller.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.utils.BarUtils;
import com.wshl.aijiuunion.view.adapter.CateAdapter;
import com.wshl.aijiuunion.view.bean.CateBean;
import com.wshl.aijiuunion.view.bean.CateListBean;
import com.wshl.aijiuunion.view.bean.EventBeans;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.baseadapter.BGAOnRVItemClickListener;

public class VideoSelectActivity extends Activity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.recycle_fenlei)
    RecyclerView recycleFenlei;
    @BindView(R.id.recycle_shenfen)
    RecyclerView recycleShenfen;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindArray(R.array.types)
    String[] types;

    private RequestModel model;

    private int type = 0;
    private int cateid = 0;

    private List<CateBean> listdata1;
    private List<CateBean> listdata2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videoselect);
        ButterKnife.bind(this);
        tvTopTitle.setText("筛选");
        BarUtils.setWhiteStatusBarForActivity(this);
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        cateid = intent.getIntExtra("cateid", 0);
        recycleShenfen.setLayoutManager(new GridLayoutManager(this, 3));
        recycleFenlei.setLayoutManager(new GridLayoutManager(this, 3));
        model = new RequestModel();
        setshenfen();
        setFenlei();

    }

    private void setFenlei() {
        listdata1 = new ArrayList<>();
        model.videoCate(this, new RequestCallback() {
            @Override
            public void onSuccess(String response) {
                CateListBean cateListBean = GsonUtils.fromJson(response, CateListBean.class);
                List<CateListBean.DataBean> data = cateListBean.getData();
                if (data.size() != 0) {

                    if (cateid == 0) {
                        listdata1.add(new CateBean(0, "全部", 1));
                    } else {
                        listdata1.add(new CateBean(0, "全部", 0));
                    }

                    for (CateListBean.DataBean datum : data) {
                        if (cateid == datum.getId()) {
                            listdata1.add(new CateBean(datum.getId(), datum.getCat_name(), 1));
                        } else {
                            listdata1.add(new CateBean(datum.getId(), datum.getCat_name(), 0));
                        }
                    }


                    CateAdapter cateAdapter = new CateAdapter(recycleFenlei);
                    cateAdapter.setData(listdata1);
                    cateAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
                        @Override
                        public void onRVItemClick(ViewGroup parent, View itemView, int position) {

                            for (CateBean cateBean : listdata1) {
                                cateBean.setStatus(0);
                            }
                            CateBean cateBean = listdata1.get(position);
                            cateBean.setStatus(1);
                            cateAdapter.notifyDataSetChanged();
                            cateid = cateBean.getId();

                        }
                    });
                    recycleFenlei.setAdapter(cateAdapter);
                }

            }

            @Override
            public void onError(int code) {

            }

            @Override
            public void onIntentError() {

            }
        });
    }

    private void setshenfen() {
        listdata2 = new ArrayList<>();
        for (int i = 0; i < types.length; i++) {
            if (type == i) {
                listdata2.add(new CateBean(i, types[i], 1));
            } else {
                listdata2.add(new CateBean(i, types[i], 0));
            }

        }
        CateAdapter cateAdapter2 = new CateAdapter(recycleShenfen);
        cateAdapter2.setData(listdata2);
        cateAdapter2.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
            @Override
            public void onRVItemClick(ViewGroup parent, View itemView, int position) {

                for (CateBean cateBean : listdata2) {

                    cateBean.setStatus(0);
                }
                listdata2.get(position).setStatus(1);
                cateAdapter2.notifyDataSetChanged();
                type = listdata2.get(position).getId();
            }
        });
        recycleShenfen.setAdapter(cateAdapter2);
    }


    @OnClick({R.id.img_back, R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_cancel://重置
                type = 0;
                cateid = 0;
                setshenfen();
                setFenlei();
                break;
            case R.id.tv_confirm://确定
                Intent intent = new Intent();
                intent.putExtra("type", type);
                intent.putExtra("cateid", cateid);
                setResult(RESULT_OK, intent);
                VideoSelectActivity.this.finish();

                break;
        }
    }


}
